/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.catalogos.prueba.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author 874836
 */
public class Conexion {

    public Connection getConexion() throws ClassNotFoundException, SQLException {
        Connection conexion = null;

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conexion = DriverManager.getConnection("jdbc:oracle:thin:@10.0.1.118:1521:xe", "desarrollo", "12345");

        } catch (ClassNotFoundException e) {
            throw new ClassNotFoundException("getConexion:ClassNotFoundException[" + e.getMessage() + "]", e);
        } catch (SQLException e) {
            throw new SQLException("getConexion:SQLException[" + e.getMessage() + "]", e);
        }

        return conexion;
    }


    public ResultSet getStatement(String query)throws SQLException, Exception{
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            cn=getConexion();
            st = cn.createStatement();
            rs = st.executeQuery(query);

        } catch (SQLException e) {
            throw new SQLException("getStatement:SQLException[" + e.getMessage() + "]", e);
        } catch (Exception e) {
            throw new Exception("getStatement:Exception[" + e.getMessage() + "]", e);
        }
        return rs;
    }


    public PreparedStatement getPreparedStatement(String query)throws SQLException, Exception{
        Connection cn = null;
        PreparedStatement  pst=null;

        try {
            cn=getConexion();
            pst = cn.prepareStatement(query);
            
        } catch (SQLException e) {
            throw new SQLException("getStatement:SQLException[" + e.getMessage() + "]", e);
        } catch (Exception e) {
            throw new Exception("getStatement:Exception[" + e.getMessage() + "]", e);
        }
        return pst;
    }
}
